package com.zhaojy.poc.service.impl;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.zhaojy.poc.frame.GpioEntity;
import com.zhaojy.poc.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zhaojiuyang1 on 16/3/12.
 */
public class CarServiceImpl implements CarService {
    @Autowired
    private GpioEntity gpioEntity;

    private GpioController gpioController;
    //11与12为左侧两个电机
    private GpioPinDigitalOutput pin11;
    private GpioPinDigitalOutput pin12;
    //22与21为右侧两个电机
    private GpioPinDigitalOutput pin21;
    private GpioPinDigitalOutput pin22;

    @Override
    public void forward() {
        pin11.low();
        pin12.high();
        pin21.high();
        pin22.low();
    }

    @Override
    public void backward() {
        pin11.high();
        pin12.low();
        pin21.low();
        pin22.high();
    }

    @Override
    public void left() {
        pin11.high();
        pin12.low();
        pin21.high();
        pin22.low();
    }

    @Override
    public void right() {
        pin11.low();
        pin12.high();
        pin21.low();
        pin22.high();
    }

    @Override
    public void stop() {
        pin11.low();
        pin12.low();
        pin21.low();
        pin22.low();
    }

    @Override
    public void init() {
        gpioController = gpioEntity.getGpioController();
        pin11 = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_00,"car11", PinState.LOW);
        pin11.setShutdownOptions(true,PinState.LOW);
        pin12 = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01,"car12", PinState.LOW);
        pin21 = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_02,"car21", PinState.LOW);
        pin22 = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_03,"car22", PinState.LOW);
        pin12.setShutdownOptions(true,PinState.LOW);
        pin21.setShutdownOptions(true,PinState.LOW);
        pin22.setShutdownOptions(true,PinState.LOW);
        GpioPinDigitalOutput enableA = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_04,"ena",PinState.HIGH);
        GpioPinDigitalOutput enableB = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_05,"enb",PinState.HIGH);
    }
}
